﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ComponentFactory.Krypton.Toolkit;
using StableDiffusionTools.Framework;

namespace StableDiffusionTools.Forms
{
    public partial class MultiEntrySelectionDialog : KryptonForm
    {
        public List<string> Result { get; private set; } = new List<string>();
        public bool AllowEmpty;
        private bool doValidation = true;

        public MultiEntrySelectionDialog(string text, string caption, List<string> inputDefault, bool allowEmpty = false)
        {
            InitializeComponent();

            Size newSize = TextRenderer.MeasureText(text, Font, new Size(1000, 1000), TextFormatFlags.Default);
            ClientSize = new Size(Math.Max(ClientSize.Width, newSize.Width), newSize.Height + ClientSize.Height);
            kryptonLBLHeader.Text = text;
            if (!string.IsNullOrEmpty(caption))
                Text = caption;

            AllowEmpty = allowEmpty;

            doValidation = false;
            for (int i = 0; i < inputDefault.Count; i++)
            {
                var node = kryptonTreeView1.Nodes.Add(inputDefault[i]);
                node.Checked = true;
            }
            doValidation = true;

            ValidateEmpty();

            KeyDown += (s, e) =>
            {
                if (e.KeyCode == Keys.Escape)
                {
                    DialogResult = DialogResult.Cancel;
                }
                else if (e.KeyCode == Keys.Enter && (AllowEmpty ? true : HasSelection()))
                {
                    UpdateResult();
                    DialogResult = DialogResult.OK;
                }
            };

            StartPosition = FormStartPosition.CenterParent;
        }

        private void kryptonBTNOk_Click(object sender, EventArgs e)
        {
            UpdateResult();
        }

        private void kryptonButtonSelectAll_Click(object sender, EventArgs e)
        {
            SelectAll(true);
        }

        private void kryptonButtonDeselectAll_Click(object sender, EventArgs e)
        {
            SelectAll(false);
        }

        private void kryptonTreeView1_AfterCheck(object sender, TreeViewEventArgs e)
        {
            ValidateEmpty();
        }

        private void SelectAll(bool select)
        {
            doValidation = false;
            for (int i = 0; i < kryptonTreeView1.Nodes.Count; i++)
            {
                kryptonTreeView1.Nodes[i].Checked = select;
            }
            doValidation = true;
            ValidateEmpty();
        }

        private void ValidateEmpty()
        {
            if (!doValidation)
                return;

            bool okEnabled = AllowEmpty ? true : HasSelection();
            kryptonBTNOk.Enabled = okEnabled;
            kryptonTreeView1.Invalidate();
        }

        private void UpdateResult()
        {
            Result = new List<string>();
            for (int i = 0; i < kryptonTreeView1.Nodes.Count; i++)
            {
                var node = kryptonTreeView1.Nodes[i];
                if (node.Checked)
                {
                    Result.Add(node.Text);
                }
            }
        }

        private bool HasSelection()
        {
            bool result = false;
            for (int i = 0; i < kryptonTreeView1.Nodes.Count; i++)
            {
                var node = kryptonTreeView1.Nodes[i];
                if (node.Checked)
                {
                    result = true;
                    break;
                }
            }
            return result;
        }

        [Obsolete("This is not supported in this class.", true)]
        public new void Show(IWin32Window window) { }
        [Obsolete("This is not supported in this class.", true)]
        public new void Show() { }
    }
}
